package com.meepo.usercenter.list

import com.lxj.xpopup.XPopup
import com.lxj.xpopup.interfaces.OnConfirmListener
import com.meepo.basic.router.RouterManager
import com.meepo.basic.router.RouterManager.UserModule.UserListFragment_Path
import com.meepo.basic.components.listfragment.MeepoListFragment
import com.meepo.basic.schema.common.PageResult
import com.meepo.basic.schema.common.ResponseWrapper
import com.meepo.basic.schema.user.User
import com.meepo.framework.design.button.SlickButton
import com.meepo.framework.design.desc.Data
import com.meepo.framework.design.desc.DescLayout
import com.meepo.framework.design.recycler.ViewHolder
import com.meepo.usercenter.R
import com.meepo.usercenter.API
import com.xiaojinzi.component.anno.FragmentAnno
import com.xiaojinzi.component.support.ParameterSupport
import io.reactivex.Observable

@FragmentAnno(UserListFragment_Path)
class UserListFragment : MeepoListFragment<User>() {

    override fun bind(item: User, holder: ViewHolder, position: Int) {

        // 用户信息
        holder.getView<DescLayout>(R.id.userinfo_view)?.let {
            it.setData(Data(title = item.nickname ?: "", avatar = item.avatar ?: ""))
            it.setOnClickListener {
                item.id?.let {
                    RouterManager.UserModule.routeToUserProfile(requireContext(), it)
                }
            }
        }


        // 关注按钮
        holder.getView<SlickButton>(R.id.extra_btn)?.let {
            if (item.followed) {
                it.type = SlickButton.BUTTON_TYPE_DEFAULT
                it.color = requireContext().resources.getColor(R.color.sub_text_color)
                it.text = "已关注"
            } else {
                it.type = SlickButton.BUTTON_TYPE_PRIMARY
                it.color = requireContext().resources.getColor(R.color.primary_color)
                it.text = "关注"
            }

            it.setOnClickListener {

                if (item.followed) {
                    XPopup.Builder(context)
                        .isDestroyOnDismiss(true)
                        .asConfirm(
                            "", """确定要取消关注吗 #${item.nickname} 吗?""".trimIndent(),
                            "再想想", "取消",

                            OnConfirmListener {
                                item.id?.let { userId ->
                                    presenter.call(
                                        API.instance().deleteRelation(User.RELATION_TYPE_FOLLOW, userId),
                                        { this.replace(position, item.setUnfollowed()) }
                                    )
                                }

                            }, null, false, com.meepo.feed.R.layout.topic_list_item_confirm_popup
                        )
                        .show()


                } else {
                    item.id?.let { userId ->
                        presenter.call(
                            API.instance().addRelation(User.RELATION_TYPE_FOLLOW, userId),
                            { this.replace(position, item.setFollowed()) }
                        )
                    }
                }

            }
        }
    }

    override fun getItemViewLayoutId(item: User, position: Int): Int {
        return R.layout.user_list_item
    }

    override fun getItemSkeletonViewLayoutId(): Int {
        return R.layout.default_item_skeleton_layout
    }

    override fun loadDataObservable(page: Int): Observable<ResponseWrapper<PageResult<User>>> {
        val relationType = ParameterSupport.getString(requireArguments(), "relationType") ?: ""
        val keyword = ParameterSupport.getString(requireArguments(), "keyword") ?: ""
        val followUserId = ParameterSupport.getString(requireArguments(), "followUserId") ?: ""

        return API.instance().getUsers(relationType, keyword, followUserId, page)
    }
}